package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;

import org.json.JSONObject;


/**
 * Created by JHS on 2015/9/10
 * 实名认证审核中
 */
public class CertificationingActivity extends BaseActivity {


    private TextView certificationName;
    private TextView certificationID;
    private ImageView certificationImg;
    private String name = "";
    private String id = "";
    private String imgUrl = "";
    private String realAuthReason = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showDialog("加载中");
        getContent();
    }

    private void getContent() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.getString("name","").equals("")) {
            //从认证页面过来的
            name = bundle.getString("name");
            id = bundle.getString("id");
            id = id.substring(0, 3) + "************" + id.substring(id.length
                    () - 3);
            imgUrl = bundle.getString("img_url");
            initIngView();
            hideDialog();

        } else {
            //从外面进来的,拿服务器数据
            SelfInfoApi.getRealAuthDetail(new BaseJsonHandler(this) {
                @Override
                public void onSuccessWithData(JSONObject response) {
                    showErrorPage(false);
                    JSONObject result = response.optJSONObject("data").optJSONObject("result");
                    name = result.optString("realname");
                    String realId = result.optString("id_card");
                    if (!realId.isEmpty()) {
                        id = realId.substring(0, 3) + "************" + realId.substring(realId.length
                                () - 3);
                    }
                    imgUrl = result.optString("id_card_img");
                    realAuthReason = result.optString("real_auth_reason");
                    switch (result.optInt("real_auth_status")) {
                        case 1://待审核
                            initIngView();
                            hideDialog();
                            break;
                        case 2://未通过
                            initFailedView();
                            hideDialog();
                            break;
                        case 3://通过
                            initSucceedView();
                            hideDialog();
                            break;
                    }
                }

                @Override
                public void onFail(int statusCode, String statusText) {
                    super.onFail(statusCode, statusText);
                    showErrorPage(true);
                }
            });
        }
    }

    @Override
    public void onErrorClick() {
        getContent();
    }

    private void initIngView() {
        BaseSetContentView(R.layout.activity_certificationing);
        setTopbarName(R.string.certificationcitle);
        certificationName = (TextView) findViewById(R.id.certification_name_tv);
        certificationID = (TextView) findViewById(R.id.certification_id_tv);
        certificationImg =(ImageView)findViewById(R.id.certification_img);

        certificationName.setText(getResources().getString(R.string.certification_name) + name);
        certificationID.setText(getResources().getString(R.string.certification_Id) + id);
        Glide.with(this).load(imgUrl).into
                (certificationImg);
    }

    private void initFailedView() {
        BaseSetContentView(R.layout.activity_certification_failed);
        setTopbarName(R.string.certificationcitle);
        TextView reason = (TextView) findViewById(R.id.failed_reason1);
        reason.setText(realAuthReason);
        Button reset = (Button) findViewById(R.id.certificationc_again_btn);
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CertificationingActivity.this, CertificationActivity.class));
                finish();
            }
        });
    }

    private void initSucceedView() {
        BaseSetContentView(R.layout.activity_certification_succeed);
        setTopbarName(R.string.certificationcitle);

        certificationName = (TextView) findViewById(R.id.succeed_name_tv);
        certificationID = (TextView) findViewById(R.id.succeed_id_tv);
        certificationImg = (ImageView) findViewById(R.id.succeed_img);

        certificationName.setText(getResources().getString(R.string.certification_name) + name);
        certificationID.setText(getResources().getString(R.string.certification_Id) + id);
        Glide.with(this).load(imgUrl).into
                (certificationImg);
    }

}